
package com.hyst.common;

import java.sql.Connection;
import java.util.Properties;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

import com.hyst.annotations.PowerField;


/**
 * @Description:
 * @Date：2018年11月17日 @author： DongY
 * @version ：1.0 1.[2018年11月17日]CreatBy：dy
 */
@SuppressWarnings({"rawtypes", "unchecked"})
@Intercepts(
    {
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
    }
)
public class MyInterceptor implements Interceptor {
	@Autowired
	PowerCheckAble owerCheckAble;
	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
		Object object=invocation.getArgs()[1];
		System.out.println(object.getClass().getName());
//		object.getClass().getAnnotations()
		PowerField powerField=object.getClass().getAnnotation(PowerField.class);
		System.err.println(powerField==null);
		if (powerField!=null) {
			System.out.println(powerField.value());
		}
		System.err.println(object);
		System.err.println(owerCheckAble.getPowers());
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object arg0) {
		if (arg0 instanceof Executor) {
			return Plugin.wrap(arg0, this);
		}
		return arg0;
	}

	@Override
	public void setProperties(Properties arg0) {
		System.out.println("setProperties");

	}

	public MyInterceptor() {
		System.out.println("init ValidateInterceptor");
	}
}
